package com.kateba.android.hid;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;

import com.kateba.android.blueinput.Singleton;


public class KBLayout
{
  static final String LOG_TAG = "BlueInput::KBLAYOUT";
  static int[] MODIFIER_KEY_CODES;
  static int[] TRIALMSG_KEY_CODES;
  static int m_nLayoutID;
  private static NodeList nl;

  static
  {
    int[] arrayOfInt1 = new int[8];
    arrayOfInt1[0] = 23;
    arrayOfInt1[1] = 59;
    arrayOfInt1[2] = 57;
    arrayOfInt1[4] = 23;
    arrayOfInt1[5] = 60;
    arrayOfInt1[6] = 58;
    MODIFIER_KEY_CODES = arrayOfInt1;
    int[] arrayOfInt2 = new int[16];
    arrayOfInt2[0] = 71;
    arrayOfInt2[1] = 30;
    arrayOfInt2[2] = 40;
    arrayOfInt2[3] = 49;
    arrayOfInt2[4] = 33;
    arrayOfInt2[5] = 37;
    arrayOfInt2[6] = 42;
    arrayOfInt2[7] = 44;
    arrayOfInt2[8] = 49;
    arrayOfInt2[9] = 48;
    arrayOfInt2[10] = 62;
    arrayOfInt2[11] = 32;
    arrayOfInt2[12] = 33;
    arrayOfInt2[13] = 41;
    arrayOfInt2[14] = 43;
    arrayOfInt2[15] = 72;
    TRIALMSG_KEY_CODES = arrayOfInt2;
    m_nLayoutID = 0;
    nl = null;
  }

  static int GetAndroidKey(int paramInt)
  {
    int i = 0;
    switch (paramInt)
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 57:
    case 70:
    case 71:
    case 72:
    case 73:
    case 83:
    case 89:
    case 90:
    case 91:
    case 92:
    case 93:
    case 94:
    case 95:
    case 96:
    case 97:
    case 98:
    case 99:
    case 103:
    case 104:
    case 105:
    case 106:
    case 107:
    case 108:
    case 109:
    case 110:
    case 111:
    case 112:
    case 113:
    case 114:
    case 115:
    case 116:
    case 117:
    case 119:
    case 120:
    case 121:
    case 122:
    case 123:
    case 124:
    case 125:
    case 126:
    case 130:
    case 131:
    case 132:
    case 133:
    case 134:
    case 135:
    case 136:
    case 137:
    case 138:
    case 139:
    case 140:
    case 141:
    case 142:
    case 143:
    case 144:
    case 145:
    case 146:
    case 147:
    case 148:
    case 149:
    case 150:
    case 151:
    case 152:
    case 153:
    case 154:
    case 155:
    case 156:
    case 157:
    case 158:
    case 159:
    case 160:
    case 161:
    case 162:
    case 163:
    case 164:
    default:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
    case 26:
    case 27:
    case 28:
    case 29:
    case 30:
    case 31:
    case 32:
    case 33:
    case 34:
    case 35:
    case 36:
    case 37:
    case 38:
    case 39:
    case 40:
    case 41:
    case 42:
    case 43:
    case 44:
    case 45:
    case 46:
    case 47:
    case 48:
    case 49:
    case 50:
    case 51:
    case 52:
    case 53:
    case 54:
    case 55:
    case 56:
    case 58:
    case 59:
    case 60:
    case 61:
    case 62:
    case 63:
    case 64:
    case 65:
    case 66:
    case 67:
    case 68:
    case 69:
    case 74:
    case 75:
    case 76:
    case 77:
    case 78:
    case 79:
    case 80:
    case 81:
    case 82:
    case 84:
    case 85:
    case 86:
    case 87:
    case 88:
    case 100:
    case 101:
    case 102:
    case 118:
    case 127:
    case 128:
    case 129:
    }
//    while (true)
//    {
//     // return i;
//      i = 29;
//     // continue;
//      i = 30;
//      continue;
//      i = 31;
//      continue;
//      i = 32;
//      continue;
//      i = 33;
//      continue;
//      i = 34;
//      continue;
//      i = 35;
//      continue;
//      i = 36;
//      continue;
//      i = 37;
//      continue;
//      i = 38;
//      continue;
//      i = 39;
//      continue;
//      i = 40;
//      continue;
//      i = 41;
//      continue;
//      i = 42;
//      continue;
//      i = 43;
//      continue;
//      i = 44;
//      continue;
//      i = 45;
//      continue;
//      i = 46;
//      continue;
//      i = 47;
//      continue;
//      i = 48;
//      continue;
//      i = 49;
//      continue;
//      i = 50;
//      continue;
//      i = 51;
//      continue;
//      i = 52;
//      continue;
//      i = 53;
//      continue;
//      i = 54;
//      continue;
//      i = 8;
//      continue;
//      i = 9;
//      continue;
//      i = 10;
//      continue;
//      i = 11;
//      continue;
//      i = 12;
//      continue;
//      i = 13;
//      continue;
//      i = 14;
//      continue;
//      i = 15;
//      continue;
//      i = 16;
//      continue;
//      i = 7;
//      continue;
//      i = 66;
//      continue;
//      i = 4;
//      continue;
//      i = 67;
//      continue;
//      i = 61;
//      continue;
//      i = 62;
//      continue;
//      i = 69;
//      continue;
//      i = 70;
//      continue;
//      i = 71;
//      continue;
//      i = 72;
//      continue;
//      i = 73;
//      continue;
//      i = 68;
//      continue;
//      i = 74;
//      continue;
//      i = 75;
//      continue;
//      i = 68;
//      continue;
//      i = 55;
//      continue;
//      i = 56;
//      continue;
//      i = 76;
//      continue;
//      i = 5;
//      continue;
//      i = 3;
//      continue;
//      i = 4;
//      continue;
//      i = 6;
//      continue;
//      i = 1;
//      continue;
//      i = 2;
//      continue;
//      i = 24;
//      continue;
//      i = 25;
//      continue;
//      i = 26;
//      continue;
//      i = 27;
//      continue;
//      i = 28;
//      continue;
//      i = 82;
//      continue;
//      i = 3;
//      continue;
//      i = 19;
//      continue;
//      i = 67;
//      continue;
//      i = 4;
//      continue;
//      i = 20;
//      continue;
//      i = 22;
//      continue;
//      i = 21;
//      continue;
//      i = 20;
//      continue;
//      i = 19;
//      continue;
//      i = 76;
//      continue;
//      i = 17;
//      continue;
//      i = 69;
//      continue;
//      i = 81;
//      continue;
//      i = 66;
//      continue;
//      i = 73;
//      continue;
//      i = 84;
//      continue;
//      i = 26;
//      continue;
//      i = 82;
//      continue;
//      i = 91;
//      continue;
//      i = 24;
//      continue;
//      i = 25;
//    }
	return i;
  }

  public static int GetAndroidKeySymbol(int paramInt)
  {
    int i = 0;
    switch (paramInt)
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 40:
    case 41:
    case 42:
    case 43:
    case 57:
    case 58:
    case 59:
    case 60:
    case 61:
    case 62:
    case 63:
    case 64:
    case 65:
    case 66:
    case 67:
    case 68:
    case 69:
    case 70:
    case 71:
    case 72:
    case 73:
    case 74:
    case 75:
    case 76:
    case 77:
    case 78:
    case 79:
    case 80:
    case 81:
    case 82:
    case 83:
    case 88:
    case 89:
    case 90:
    case 91:
    case 92:
    case 93:
    case 94:
    case 95:
    case 96:
    case 97:
    case 98:
    case 99:
    case 101:
    case 102:
    case 103:
    case 104:
    case 105:
    case 106:
    case 107:
    case 108:
    case 109:
    case 110:
    case 111:
    case 112:
    case 113:
    case 114:
    case 115:
    case 116:
    case 117:
    case 118:
    case 119:
    case 120:
    case 121:
    case 122:
    case 123:
    case 124:
    case 125:
    case 126:
    case 127:
    case 128:
    case 129:
    case 130:
    case 131:
    case 132:
    case 133:
    case 134:
    case 135:
    case 136:
    case 137:
    case 138:
    case 139:
    case 140:
    case 141:
    case 142:
    case 143:
    case 144:
    case 145:
    case 146:
    case 147:
    case 148:
    case 149:
    case 150:
    case 151:
    case 152:
    case 153:
    case 154:
    case 155:
    case 156:
    case 157:
    case 158:
    case 159:
    case 160:
    case 161:
    case 162:
    case 163:
    case 164:
    default:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
    case 26:
    case 27:
    case 28:
    case 29:
    case 30:
    case 31:
    case 32:
    case 33:
    case 34:
    case 35:
    case 36:
    case 37:
    case 38:
    case 39:
    case 44:
    case 45:
    case 46:
    case 47:
    case 48:
    case 49:
    case 50:
    case 51:
    case 52:
    case 53:
    case 54:
    case 55:
    case 56:
    case 84:
    case 85:
    case 86:
    case 87:
    case 100:
    }
//    while (true)
//    {
      return i;
//      i = 65;
//      continue;
//      i = 66;
//      continue;
//      i = 67;
//      continue;
//      i = 68;
//      continue;
//      i = 69;
//      continue;
//      i = 70;
//      continue;
//      i = 71;
//      continue;
//      i = 72;
//      continue;
//      i = 73;
//      continue;
//      i = 74;
//      continue;
//      i = 75;
//      continue;
//      i = 76;
//      continue;
//      i = 77;
//      continue;
//      i = 78;
//      continue;
//      i = 79;
//      continue;
//      i = 80;
//      continue;
//      i = 81;
//      continue;
//      i = 82;
//      continue;
//      i = 83;
//      continue;
//      i = 84;
//      continue;
//      i = 85;
//      continue;
//      i = 86;
//      continue;
//      i = 87;
//      continue;
//      i = 88;
//      continue;
//      i = 89;
//      continue;
//      i = 90;
//      continue;
//      i = 49;
//      continue;
//      i = 50;
//      continue;
//      i = 51;
//      continue;
//      i = 52;
//      continue;
//      i = 53;
//      continue;
//      i = 54;
//      continue;
//      i = 55;
//      continue;
//      i = 56;
//      continue;
//      i = 57;
//      continue;
//      i = 48;
//      continue;
//      i = 32;
//      continue;
//      i = 45;
//      continue;
//      i = 61;
//      continue;
//      i = 91;
//      continue;
//      i = 93;
//      continue;
//      i = 92;
//      continue;
//      i = 126;
//      continue;
//      i = 59;
//      continue;
//      i = 39;
//      continue;
//      i = 96;
//      continue;
//      i = 44;
//      continue;
//      i = 46;
//      continue;
//      i = 47;
//      continue;
//      i = 47;
//      continue;
//      i = 42;
//      continue;
//      i = 45;
//      continue;
//      i = 43;
//      continue;
//      i = 92;
//    }
  }

  static int GetLayoutKey(int paramInt, boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3)
  {
    int i = 0;
    Element localElement = null;
    if ((nl != null) && (paramInt < nl.getLength()) && (paramInt > 0))
    {
      localElement = (Element)nl.item(paramInt);
     // if (localElement != null)
//        break label47;
    }
    while (true)
    {
    //  return i;
//      label47:
      String str1 = localElement.getAttribute("normal");
      String str2 = localElement.getAttribute("shift");
      String str3 = localElement.getAttribute("alt");
      String str4 = localElement.getAttribute("altshift");
      if ((paramBoolean2) && (paramBoolean1))
      {
        i = Integer.parseInt(str4, 16);
        continue;
      }
      if (paramBoolean1)
      {
        i = Integer.parseInt(str2, 16);
        continue;
      }
      if (paramBoolean2)
      {
        i = Integer.parseInt(str3, 16);
        continue;
      }
      i = Integer.parseInt(str1, 16);
    }
  }

  public static int LoadLayout(Context paramContext, int paramInt)
  {
    m_nLayoutID = paramInt;
    Singleton.getInstance().Debug("BlueInput::KBLAYOUT", "LoadLayout: ID:" + paramInt);
    int i = 0;
    switch (paramInt)
    {
    default:
      i = 0;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    }
//    while (true)
//    {
//      return i;
//      i = LoadLayout(paramContext, "english.xml");
//      continue;
//      i = LoadLayout(paramContext, "french.xml");
//      continue;
//      i = LoadLayout(paramContext, "german.xml");
//      continue;
//      i = LoadLayout(paramContext, "italian.xml");
//      continue;
//      i = LoadLayout(paramContext, "norwegian.xml");
//      continue;
//      i = LoadLayout(paramContext, "romanian.xml");
//      continue;
//      i = LoadLayout(paramContext, "russian.xml");
//      continue;
//      i = LoadLayout(paramContext, "spanish.xml");
//      continue;
//      i = LoadLayout(paramContext, "swedish.xml");
//    }
	return i;
  }

  public static int LoadLayout(Context paramContext, String paramString)
  {
    int i = -1;
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    try
    {
      Singleton.getInstance().Debug("BlueInput::KBLAYOUT", "LoadLayout ResAssetName:" + paramString);
      Document localDocument = localDocumentBuilderFactory.newDocumentBuilder().parse(paramContext.getAssets().open(paramString));
      if (localDocument != null)
      {
        Element localElement = localDocument.getDocumentElement();
        if (localElement != null)
        {
          nl = localElement.getElementsByTagName("key");
          if ((nl != null) && (nl.getLength() > 0))
          {
            Singleton.getInstance().Debug("BlueInput::KBLAYOUT", "LoadLayout ok, keys defined:" + nl.getLength());
            i = 0;
          }
        }
      }
    }
    catch (ParserConfigurationException localParserConfigurationException)
    {
      localParserConfigurationException.printStackTrace();
    }
    catch (SAXException localSAXException)
    {
      localSAXException.printStackTrace();
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
    return i;
  }
}